// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.chimi.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.chimi.controller.request.QueryBarPointRuleReq;
import com.liuxinlong.modules.chimi.controller.request.UpdateBarPointRuleDetailReq;
import com.liuxinlong.modules.chimi.controller.request.UpdateBarPointsRuleReq;
import com.liuxinlong.modules.chimi.service.BarPointsRuleService;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.BarPointsRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 论坛积分规则控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2023-06-14
 */
@RestController
@RequestMapping("/points-rule")
public class BarPointsRuleController {
    @Autowired
    private BarPointsRuleService barPointsRuleService;

    /**
     * 分页查询积分规则信息
     *
     * @param req 请求信息
     * @return 积分规则信息列表
     */
    @PostMapping("page")
//	@LogAnnotation(value ="分页查询",business = LogBusinessConstants.BAR_POINTS_RULE,operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp pagePointsRuleInfo(@RequestBody QueryBarPointRuleReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("name", req.getName());
        queryParam.put("startTime", req.getStartTime());
        queryParam.put("endTime", req.getEndTime());
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        List<Map<String, Object>> dataList = barPointsRuleService.pagePointsRule(queryParam);
        int total = barPointsRuleService.getPointsRuleCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setTotalNum(total);
        resp.setRetMessage("分页查询积分规则信息成功");
        return resp;
    }

    /**
     * 新增积分规则
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value = "新增积分规则", business = LogBusinessConstants.BAR_POINTS_RULE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp addPointsRule(@RequestBody UpdateBarPointsRuleReq req) {
        BarPointsRule barPointsRule = new BarPointsRule();
        barPointsRule.setName(req.getName());
        barPointsRule.setStartTime(req.getStartTime());
        barPointsRule.setEndTime(req.getEndTime());
        barPointsRule.setRemark(req.getRemark());
        barPointsRuleService.addPointsRule(barPointsRule);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增积分规则成功");
        return resp;
    }

    /**
     * 修改积分规则信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value = "修改积分规则信息", business = LogBusinessConstants.BAR_POINTS_RULE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updatePointsRule(@RequestBody UpdateBarPointsRuleReq req) {
        BarPointsRule barPointsRule = new BarPointsRule();
        barPointsRule.setId(req.getId());
        barPointsRule.setName(req.getName());
        barPointsRule.setStartTime(req.getStartTime());
        barPointsRule.setEndTime(req.getEndTime());
        barPointsRule.setRemark(req.getRemark());
        barPointsRuleService.updatePointsRule(barPointsRule);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改积分规则信息成功");
        return resp;
    }

    /**
     * 删除积分规则信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除积分规则信息", business = LogBusinessConstants.BAR_POINTS_RULE, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deletePointsRule(@RequestBody BaseDeleteReq req) {
        barPointsRuleService.deletePointsRule(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除积分规则信息成功");
        return resp;
    }

    /**
     * 修改积分规则状态
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("status-update")
    @LogAnnotation(value = "修改积分规则状态", business = LogBusinessConstants.BAR_POINTS_RULE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updatePointsRuleStatus(@RequestBody QueryBaseReq req) {
        barPointsRuleService.updatePointsRuleStatus(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改积分规则状态成功");
        return resp;
    }

    /**
     * 查询积分规则详细规则
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("detail-page")
    public BaseResp pagePointsRuleDetail(@RequestBody QueryBaseReq req) {
        List<Map<String,Object>> dataList = barPointsRuleService.pagePointsRuleDetail(req.getId());
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setRetMessage("询积分规则详细规则成功");
        return resp;
    }

    /**
     * 修改积分规则详细规则
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("detail-update")
    @LogAnnotation(value = "修改积分规则详细规则", business = LogBusinessConstants.BAR_POINTS_RULE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updatePointsRuleDetail(@RequestBody UpdateBarPointRuleDetailReq req) {
        barPointsRuleService.updatePointsRuleDetail(req.getId(), req.getDataList());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改积分规则详细规则成功");
        return resp;
    }
}
